#include <stdio.h>
#include <stdlib.h>
#include "creat.h"
void primorder(tree *root)
{
    if (root)
    {
        printf("%3d", root->data);
        primorder(root->left);
        primorder(root->right);
    }
}
void stack_primorder(tree *root)
{
    tree *stack[50];
    int top = -1;
    tree *p = root;
    while (p || top >= 0)
    {
        while (p)
        {
            stack[++top] = p;
            printf("%3d", p->data);
            p = p->left;
        }
        if (top >= 0)
        {
            p = stack[top--];
            p = p->right;
        }
    }
}
void reorder(tree *root)
{
    tree *stack[50];
    int top = -1;
    int flag[50] = {0};
    tree *p = root;

    while (top >= 0 || p)
    {
        while (p)
        {
            stack[++top] = p;
            flag[top]++;
            p = p->left;
        }
        switch (flag[top])
        {
        case 1:
            if (stack[top]->right)
            {
                p = stack[top]->right;
            }
            flag[top]++;
            break;
        case 2:
            printf("%3d", stack[top]->data);
            flag[top] = 0;
            top--;
            break;
        default:
            break;
        }
    }
}
int main()
{
    /*******************code*******************/
    tree *root = creat();
    primorder(root);
    printf("\n");
    stack_primorder(root);
    printf("\n");
    reorder(root);
    /******************************************/
    printf("\n\n****************************\n");
    printf("Press Enter key to continue\n");
    getchar();
    return 0;
    /******************************************/
}
